package com.xxgc.vfc.auth.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.crypto.digest.DigestAlgorithm;
import cn.hutool.crypto.digest.Digester;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xxgc.vfc.api.dto.UserLoginDTO;
import com.xxgc.vfc.api.vo.PhoneAndPassLoginVO;
import com.xxgc.vfc.api.vo.UserPhoneRegisterVO;
import com.xxgc.vfc.auth.entity.Users;
import com.xxgc.vfc.auth.mapper.UsersMapper;
import com.xxgc.vfc.auth.service.IUsersService;
import com.xxgc.vfc.common.error.center.custom.PhoneOrPassException;
import com.xxgc.vfc.common.error.center.custom.UserPhoneAlreadyExistsException;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户 服务实现类
 * </p>
 *
 * @author 风清扬
 * @since 2023-11-10
 */
@Service
public class UsersServiceImpl extends ServiceImpl<UsersMapper, Users> implements IUsersService {

    /**
     * 1、先写一个获取手机验证码的方法
     * 2、生成一个验证码
     * 3、把验证码给移动，让移动给这个手机发个验证码
     * 4、生成一个UUID 把UUID当做key 验证码当做value存入到redis 设置60S失效
     * 5、返回UUID给前端
     * 6、前端隐藏UUID，在提交注册表单时携带上UUID
     */

    @Override
    public boolean userPhoneRegister(UserPhoneRegisterVO userPhoneRegisterVO) {
        /**
         * 1、收到一个UUID和一个手机验证码
         * 2、UUID在redis中获取出验证码
         * 3、对比两个验证码
         */

        //验证手机号是否被注册
        QueryWrapper<Users> qw = new QueryWrapper<>();
        //加条件
        qw.eq("phone_number",userPhoneRegisterVO.getPhoneNumber());
        Users one = this.getOne(qw);
        if(ObjectUtil.isNotNull(one)){
            //报手机号已存在异常
            throw new UserPhoneAlreadyExistsException().errorMessage("手机号已被注册！");
        }


        //对密码进行MD5非对称加密
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        String passWord = userPhoneRegisterVO.getPassWord();
        //随机盐值
        //获取UUID
        String simpleUUID = IdUtil.simpleUUID().substring(0,20);
        //加盐
        md5.setSalt(simpleUUID.getBytes());
        //算密码
        String digestHex = md5.digestHex(passWord);
        userPhoneRegisterVO.setPassWord(digestHex);
        Users users = new Users();
        BeanUtil.copyProperties(userPhoneRegisterVO, users);
        //盐值存数据库
        users.setSalt(simpleUUID);
        System.out.println("users = " + users.toString());
        //添加用户
        this.save(users);
        return true;
    }

    //手机号密码登录业务逻辑
    @Override
    public UserLoginDTO userPhoneAndPassLogin(PhoneAndPassLoginVO phoneAndPassLoginVO) {
        //通过手机号查询出盐值
        QueryWrapper<Users> qw = new QueryWrapper<>();
        //加条件
        qw.eq("phone_number",phoneAndPassLoginVO.getPhoneNumber());
        Users userEntity = this.getOne(qw);
        if(!ObjectUtil.isNotNull(userEntity)){
            //  防止社会工程学
            throw new PhoneOrPassException().errorMessage("手机号或密码错误");
        }
        //通过手机号获取到之前加密用的盐
        String salt = userEntity.getSalt();
        //数据库加密过的密码
        String passWord = userEntity.getPassWord();
        //没有加密密码
        String unPass = phoneAndPassLoginVO.getPassWord();

        //加密
        //对密码进行MD5非对称加密
        Digester md5 = new Digester(DigestAlgorithm.MD5);
        //加盐
        md5.setSalt(salt.getBytes());
        //算密码
        String digestHex = md5.digestHex(unPass);
        //如果加密内容对不上
        if(!passWord.equals(digestHex)){
            throw new PhoneOrPassException().errorMessage("手机号或密码错误");
        }

        UserLoginDTO userLoginDTO = new UserLoginDTO();
        BeanUtil.copyProperties(userEntity,userLoginDTO);
        return userLoginDTO;
    }
}


